<?php
namespace app\common\controller;

class Marcketline
{
	
	/**
	 * 获取深度数据
	 * @param string $symbol 交易对
	 * @param int $depth 返回的深度数量
	 * @param int $type 深度的价格聚合度
	 * @return Object 
	 */
	public function deepLine($symbol,$depth,$type)
	{
		$url = "https://api.huobi.pro/market/depth?symbol={$symbol}&depth={$depth}&type={$type}";
		$res = Curls::curl($url);
		$res = json_decode($res,true);
		return $res;
	}
	
	/**
	 * 获取交易对交易记录
	 * @param string $symbol 交易对
	 * @param int $size 查询数量
	 * @return Object
	 */
	public function tradeRecord($symbol,$size)
	{
		$url = "https://api.huobi.pro/market/history/trade?symbol={$symbol}&size={$size}";
		$res = Curls::curl($url);
		$res = json_decode($res,true);
		foreach ($res['data'] as &$rows) {
			$rows['time'] = date('H:i:s',$rows['ts']);
		}
		return $res;
	}
	
	/**
	 * 返回交易对的最新行情
	 * @param string $symbol 交易对
	 * @return mixed|number
	 */
	public function getSymbolClose($symbol)
	{
		//获取交易对的行情
		$ticks = Curls::curl('https://api.huobi.pro/market/tickers');
		$data = json_decode($ticks,true)['data'];
		foreach ($data as $rows) {
			if($rows['symbol'] == $symbol){
				$num =  (($rows['close'] - $rows['open']) / $rows['open']) * 100;
				$rows['rate'] =round($num,2);
				return $rows;
			}
		}
		return false;
	}
}